#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

sh_file=$PWD/$0
sh_path=$(dirname $sh_file)
app_path=$(dirname $sh_path)
env_file=$sh_path/.env
function get_config()
{
  param=$1
  value=$(sed -E '/^#.*|^ *$/d' $env_file | awk -F "${param}=" "/${param}=/{print \$2}" | tail -n1)
  echo $value
}
tmp=$sh_path/tmp
system=$(get_config system)
app_name=$(get_config app_name)
project_name=$(get_config project_name)
docker_path=$(get_config docker_path)
win_npm_path=$(get_config win_npm_path)

echo '当前编译环境为: '$system
echo '当前项目为: '$project_name
echo '编译初始化 start'
if [ ! -d $tmp  ];then
  mkdir $tmp
fi
if [ ! -d $tmp/.gitignore  ];then
  touch $tmp/.gitignore
  echo '*' > $tmp/.gitignore
  echo '!.gitignore' >> $tmp/.gitignore
fi
rm -rf $tmp/$app_name* 
echo '编译初始化 end'
if [ $system == 'docker' ];then
  echo 'docker 环境打包 start'
  echo $docker_path
  docker exec -w $docker_path node14 npm run build:prod 
  echo 'docker 环境打包 end'
elif [ $system == 'linux' ];then
  echo 'linux 环境打包 start'
  echo 'linux 环境打包 end'
elif [ $system == 'win' ];then
  echo 'win 环境打包 start'
  cd $app_path && $win_npm_path/npm run build:prod 
  echo 'win 环境打包 end'
fi

echo 'tar 压缩 start'
cd $app_path && tar -czvf $app_name.tar.gz $app_name 
rm -rf $app_path/$app_name
mv $app_path/$app_name.tar.gz $tmp
echo 'tar 压缩 end'
